package com.augn.service.impl;

import com.augn.entity.Cart;
import com.augn.entity.Result;
import com.augn.mapper.CartMapper;
import com.augn.service.ICartService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author augn
 * @since 2022-09-22
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    @Autowired
    private CartMapper mapper;

    //新增一件购物车商品
    @Override
    public Result<?> insertOneCart(Cart cart) {
        int insert = mapper.insert(cart);
        if(insert==0){
            return Result.error("-1","新增失败");
        } else{
            return Result.success(insert);
        }
    }

    //删除一件购物车商品
    @Override
    public Result<?> deleteOneCart(Integer id) {
        int delete = mapper.deleteById(id);
        if(delete==0){
            return Result.error("-1","删除失败");
        } else{
            return Result.success(delete);
        }
    }

    //更新一件购物车商品
    @Override
    public Result<?> updateOneCart(Cart cart) {
        int update = mapper.updateById(cart);
        if(update==0){
            return Result.error("-1","更新失败");
        } else{
            return Result.success(update);
        }
    }

    @Override
    public Result<?> getPartCart(Integer id) {
        QueryWrapper<Cart> wrapper= new QueryWrapper<>();
        wrapper.eq("user_id",id);
        List<Cart> cartList = mapper.selectList(wrapper);
        if(cartList.isEmpty()){
            return Result.error("-1","空购物车");
        } else{
            return Result.success(cartList);
        }
    }
}
